N Queens
Hard
Question
Given a square chessboard of n width and height, place n queens on the board such that they do not attack each other (a queen cannot be on the same line horizontally, vertically, or diagonally as another queen).
Return all possible solutions to the n queens problem.
Note: In our solution, "Q" indicates a queen while "." indicates an empty space.
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
There are two distinct solutions to the 4-queens puzzle:
Input: n = 1
Output: [["Q"]]
There is a single solution to the 1-queen puzzle:
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
How many unique combinations should we return if given n = 2?
0
1
2
4
All test cases pass! 🎉
Time limit exceeded
InputExpected OutputActual Output
Standard OutputScroll down...
Login or signup to save your code.
Uh oh... looks like you don't yet have access.
Not sure what this unlocks? Check out a free pattern section.